﻿using System;
using System.Windows.Forms;
using Sanjeet.FastCopy.FileCopyLogic;

namespace Sanjeet.FastCopy.FileCopy
{
    public partial class Default : Form
    {
        public Default()
        {
            InitializeComponent();
            CopyProgress.Visible = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartButton_Click(object sender, EventArgs e)
        {
            try
            {
                var fileSystem = new FileSystemOperation();
                fileSystem.Overwrite = OverwriteCheckbox.Checked;
                fileSystem.SubFolders = SubFoldersCheckbox.Checked;
                fileSystem.Filter = FileExtensionsTextbox.Text;
                ResetUIOnCopyStarted(false);
                fileSystem.ProgressChanged +=
                    fileSystem_ProgressChanged;
                fileSystem.Copy(SourceFolderTextbox.Text, DestinationFolderTextbox.Text);
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ex.Message;
                ResetUIOnCopyStarted(true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fileSystem_ProgressChanged(object sender, FileSystemOperationEventArg e)
        {
            StatusLabel.Text = e.Message;
            if (e.Completed)
            {
                ResetUIOnCopyStarted(true);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enabled"></param>
        private void ResetUIOnCopyStarted(bool enabled)
        {
            CopyProgress.Visible = !enabled;
            StartButton.Enabled = enabled;
            SourceFolderTextbox.Enabled = enabled;
            DestinationFolderTextbox.Enabled = enabled;
            OverwriteCheckbox.Enabled = enabled;
            FileExtensionsTextbox.Enabled = enabled;
        }
    }
}